package skeleton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.net.URL;
import java.sql.Time;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class BoardImage extends JPanel {

	private Image bardejov = null;
	private final String PATH = "/skeleton/star.png";
	private int x = 2;
	private int y = 0;
	private double theta = 0;

	public BoardImage() {
		
	}
	
	private void loadImage() {
		URL url = getClass().getResource(PATH);
		if (url != null) {
			ImageIcon ii = new ImageIcon(url);
			bardejov = ii.getImage();
//			setPreferredSize(new Dimension(ii.getIconWidth(), ii.getIconHeight()));
			setPreferredSize(new Dimension(500, 300));
//			setBackground(Color.black);
			repaint();
			return;
		} 
		System.out.println("Loading error");
	}
	
	private void setImage(Image image) {
		this.bardejov = image;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		if (bardejov != null) {
//			g2d.drawImage(bardejov, x, 0, null);
			AffineTransform at = AffineTransform.getTranslateInstance(x, 50 + (int) (Math.sin(x) * 10));
			at.rotate(Math.toRadians(theta));
			g2d.draw(at.createTransformedShape(new Rectangle(60, 30)));
			g2d.draw(at.createTransformedShape(new Rectangle(30, 60)));
//			System.out.println(x);
		} 
//		else {
//			g2d.setColor(Color.GREEN);
//			g2d.fillRect(10, 10, 100, 100);
//		}
	}
	
	private void anime() {
		Timer time = new Timer(95, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (x < BoardImage.this.getWidth()) {
					x += (int) (Math.abs(Math.sin(x) * 10));
				} else {
					x = 2;
				}
				if (theta < 360) {
					theta += 10;
				} else {
					theta = 0;
				}
				repaint();
			}
		});
		time.start();
	}

	public static void main(String[] args) {
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				JFrame frame = new JFrame("Image");
				frame.setLayout(new BorderLayout());
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setLocationRelativeTo(null);
				BoardImage bi = new BoardImage();
				bi.loadImage();
				frame.add(bi, BorderLayout.CENTER);
				frame.pack();
				frame.setVisible(true);
				bi.anime();
			}
		};
		SwingUtilities.invokeLater(runnable);
	}
}